package com.phone580.app.splash;

import java.io.File;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.phone580.app.BaseActivity;
import com.phone580.app.BaseTask.TaskCallback;
import com.phone580.app.CommonData;
import com.phone580.app.R;
import com.phone580.app.main.MainActivity;
import com.phone580.app.untilty.AlertUtil;
import com.phone580.app.untilty.CommonUtility;

public class SplashActivity extends BaseActivity implements TaskCallback{
	public static final int CHECK_NETWORK_FLAG = 1;
	public static final int CHECK_SDCAD_FLAG = 2;
	public static final int CHECK_DTAT_VERSION_FLAG = 3;
	public static final int DOWNLOAD_DATA_FLAG = 4;
	public static final int DOWNLOAD_CONFIG_FLAG = 5;
	public static final int UNZIP_FILE_FLAG = 6;
	public static final int SUCCESS_FLAG = 7;
	public static final int ERROR_FLAG = 8;
	
	private static TextView tvTips;
	//http://www.phone580.com:8082/websamp/pubapi/checkupdate?type=FBS_MOBILE&versioncode=0&isPub=0
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			case CHECK_NETWORK_FLAG:
				tvTips.setText("检测网络中");
				break;
			case CHECK_SDCAD_FLAG:
				tvTips.setText("检测SD卡中");
				break;
			case CHECK_DTAT_VERSION_FLAG:
				tvTips.setText("检查数据包版本");
				break;
			case DOWNLOAD_CONFIG_FLAG:
				tvTips.setText("正下配置文件 " + msg.arg1);
				break;
			case DOWNLOAD_DATA_FLAG:
				tvTips.setText("正下载数据包 " + msg.arg1);
				break;
			case UNZIP_FILE_FLAG:
				tvTips.setText("解压数据包");
				break;
			case ERROR_FLAG:
				error(msg.obj.toString());
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_layout);
		tvTips = (TextView) findViewById(R.id.splash_tips_tv);
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				handler.sendEmptyMessage(CHECK_NETWORK_FLAG);
				if (!CommonUtility.isNetworkConnected(SplashActivity.this)) {
					error("网络出故障了");
					return;
				}
				handler.sendEmptyMessage(CHECK_SDCAD_FLAG);
//				File mnt = new File("/mnt");
//				 String[] files = mnt.list();
//				 for (String file : files) {
//					 if (file.contains("SDCARD") && !"SDCARD".equals(file)) {
//						 CommonData.SDCARD = "/mnt/" + file;
//					 }
//				 }
				
				 if(!(new File(CommonData.APP_PATH).exists())){
					 error("SD卡不可用，请检查SD卡");
					 return;
				 }
				 handler.sendEmptyMessage(CHECK_DTAT_VERSION_FLAG);
//				 tvTips.setText("检测数据包版本");
				 CheckUpdateTask task = new CheckUpdateTask(SplashActivity.this, handler);
				 task.setTaskCallback(SplashActivity.this);
				 task.execute();
			}
		}, 800);
	}
	
	
	
	private void error(String content) {
		AlertUtil.showAlert(SplashActivity.this, R.string.warnning, content, 
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						SplashActivity.this.finish();
					}
				});
	}



	@Override
	public void execute(Object result) {
		int flag = (Integer) result;
		if (flag == CheckUpdateTask.SUCCESS) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			finish();
		} else {
			error("发生错误，无法启动");
		}
	}
	
}
